﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;

using System.Diagnostics;
using System.Linq.Expressions;
using System.Web.Routing;
using Lilium.Web.Mvc.Context;
using Lilium.Web.Mvc.State;
using Lilium.Collections;

namespace Lilium.Web.Mvc.Html
{
	public static class HtmlExtension
	{
		//TODO: убрать отсюда, здесь должны быть только общие расширения.
		public static MvcForm BeginForm<TController>(this HtmlHelper htmlHelper, Expression<Func<TController, ActionResult>> action)
		{
			return BeginForm<TController>(htmlHelper, action, null);
		}

		//TODO: убрать отсюда, здесь должны быть только общие расширения.
		public static MvcForm BeginForm<TController>(this HtmlHelper html, Expression<Func<TController, ActionResult>> action, object routeValues)
		{
			if (html == null) throw new ArgumentNullException("html");

            var values = html.ViewContext.Controller.GetCombinedRouteValuesWithState(action, new RouteValueDictionary(routeValues));

            return html.BeginForm(values);
		}

		public static EntityController GetEntityController(this HtmlHelper html)
		{
			if (html == null) throw new ArgumentNullException("html");

			var entityController = html.ViewContext.Controller as EntityController;
			if (entityController == null)
				throw new InvalidOperationException("Entity controller not found");
			return entityController;
		}

		public static TService GetService<TService>(this HtmlHelper html, bool required)
		{
			var serviceProvider = GetEntityController(html);
			return serviceProvider.GetService<TService>(required);
		}

		public static object  GetService(this HtmlHelper html, Type serviceType, bool required)
		{
			var serviceProvider = GetEntityController(html);
			return serviceProvider.GetService(serviceType, required);
		}
        /*
		public static RouteValueDictionary GetCombinedRouteValuesWithState(this HtmlHelper html)
		{
			var actionContextService = html.GetService<IActionContextService>(true);
			var routeValues = actionContextService.GetCombinedRouteValues();

			var stateService = html.GetService<IStateService>(false);
			if (stateService != null)
				stateService.SaveObjectsState(routeValues);

			return routeValues;
		}
        */

		//TODO: убрать отсюда, здесь должны быть только общие расширения.
		public static MvcHtmlString Text(this HtmlHelper htmlHelper, string value)
		{
			if (!string.IsNullOrEmpty(value))
			{
                value = value.Replace("\r\n", "<br />").Replace("\n", "<br />");
				return MvcHtmlString.Create(value);
			}
			else
				return MvcHtmlString.Empty;
		}

        public static string GetFullHtmlFieldName<TModel, TResult>(this HtmlHelper<TModel> html, Expression<Func<TModel, TResult>> expression)
        {
            if (html == null) throw new ArgumentNullException("html");

            var expressionText = ExpressionHelper.GetExpressionText(expression);
            var htmlFieldName = html.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionText);

            return htmlFieldName;
        }

		public static string GetValidationMessage(this HtmlHelper htmlHelper)
		{
			if (htmlHelper == null) throw new ArgumentNullException("htmlHelper");

			return htmlHelper.ViewData.GetValidationMessage();
		}
	}
}
